<?php 
include('../conexion.php');

$fechade = $_POST['fechade'];
$fechaa  = $_POST['fechaa'];
$tipokar = $_POST['tipokar'];

if($tipokar=='')
{
	$consulta = mysql_query("SELECT IF(DATE_FORMAT(kardex.fechaescritura,'%d/%m/%Y')='00/00/0000','',DATE_FORMAT(kardex.fechaescritura,'%d/%m/%Y')) AS 'fec_escritura_2', tipokar.nomtipkar AS 'tipo'  ,kardex.*   FROM kardex INNER JOIN tipokar ON tipokar.idtipkar = kardex.idtipkar WHERE kardex.fechaconclusion='' AND (fechaescritura BETWEEN '$desde' AND '$hasta') order by kardex.kardex desc", $conn) or die(mysql_error());
}
	else
	{
		$consulta = mysql_query("SELECT tipokar.nomtipkar AS 'tipo'  ,kardex.*   FROM kardex INNER JOIN tipokar ON tipokar.idtipkar = kardex.idtipkar WHERE kardex.idtipkar='$tipokar' AND kardex.fechaconclusion='' AND (fechaescritura BETWEEN '$desde' AND '$hasta') order by fechaescritura asc", $conn) or die(mysql_error());
			
	}
while($row = mysql_fetch_array($consulta)){

echo"<table width='834' border='1' cellpadding='0' cellspacing='0' >
  <tr>
    <td width='100' valign='top'><span class='Estilo12'>".$row['tipo']."</span></td>
	<td width='50' valign='top'><span class='Estilo12'>".$row['kardex']."</span></td>
    <td width='70' height='19' valign='top'><span class='Estilo12'>". $row['fec_escritura_2']."</span></td>
	  <td width='93' valign='top'><span class='Estilo12'>".$row['numescritura']."</span></td>
	  <td width='171' valign='top'><span class='Estilo12'>".$row['contrato']."</span></td>
    <td width='275' valign='top'><span class='Estilo12'>"; 
	$kardex=$row['kardex'];
	$consultr = mysql_query("SELECT * FROM contratantes WHERE kardex='$kardex' and indice='1'", $conn) or die(mysql_error());
	while($row2 = mysql_fetch_array($consultr)){
	  $contratante=$row2['idcontratante'];
	  $consultrr = mysql_query("SELECT * FROM cliente2 WHERE idcontratante='$contratante' order by idcontratante asc", $conn) or die(mysql_error());
	  while($row3 = mysql_fetch_array($consultrr)){
	   if($row3['tipper']=="N") { echo $row3['apepat']." ".$row3['apemat'].", ".$row3['prinom']." ".$row3['segnom']."<br>";  }else {  echo str_replace("*","&",str_replace("?","'",$row3['razonsocial']))."<br>"; }
	
	  }
	}
	echo"</span></td>
  </tr>
</table>";
}

?>